<?php

declare(strict_types=1);

use think\migration\Migrator;
use think\migration\db\Column;

class CreateUserTables extends Migrator
{
    public function change(): void
    {
        $this->createUserAccountTable();
        $this->createUserProfileTable();
        $this->createUserAuthTable();
        $this->createUserRefreshTokenTable();
        $this->createUserVerificationCaseTable();
        $this->createUserIdentityTable();
        $this->createUserIndividualTable();
        $this->createUserCompanyTable();
    }

    private function createUserAccountTable(): void
    {
        $this->table('user_account', [
            'comment' => '用户账户表（登录主体）',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('phone', 'string', [
                'limit' => 20,
                'null' => true,
                'comment' => '手机号',
            ])
            ->addColumn('email', 'string', [
                'limit' => 120,
                'null' => true,
                'comment' => '邮箱',
            ])
            ->addColumn('password_hash', 'string', [
                'limit' => 255,
                'null' => true,
                'comment' => '密码哈希',
            ])
            ->addColumn('status', 'tinyinteger', [
                'default' => 1,
                'signed' => false,
                'comment' => '状态 0-禁用 1-启用 2-冻结',
            ])
            ->addColumn('invited_by', 'integer', [
                'null' => true,
                'signed' => false,
                'comment' => '邀请人用户ID',
            ])
            ->addColumn('register_channel', 'string', [
                'limit' => 50,
                'default' => 'manual',
                'comment' => '注册渠道',
            ])
            ->addColumn('primary_region_id', 'integer', [
                'null' => true,
                'signed' => false,
                'comment' => '默认区域ID',
            ])
            ->addColumn('last_login_time', 'datetime', [
                'null' => true,
                'comment' => '最后登录时间',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['phone'], ['unique' => true])
            ->addIndex(['email'], ['unique' => true])
            ->addIndex(['invited_by'])
            ->addIndex(['primary_region_id'])
            ->create();
    }

    private function createUserProfileTable(): void
    {
        $this->table('user_profile', [
            'comment' => '用户资料表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('nickname', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '昵称',
            ])
            ->addColumn('real_name', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '真实姓名',
            ])
            ->addColumn('avatar', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '头像',
            ])
            ->addColumn('gender', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '性别 0-未知 1-男 2-女',
            ])
            ->addColumn('id_card', 'string', [
                'limit' => 32,
                'null' => true,
                'comment' => '身份证号',
            ])
            ->addColumn('extra', 'json', [
                'null' => true,
                'comment' => '扩展信息',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['user_id'], ['unique' => true])
            ->create();
    }

    private function createUserAuthTable(): void
    {
        $this->table('user_auth', [
            'comment' => '用户认证凭证表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('identity_type', 'string', [
                'limit' => 30,
                'comment' => '认证类型（phone/wechat等）',
            ])
            ->addColumn('identifier', 'string', [
                'limit' => 255,
                'comment' => '认证标识（手机号、openId等）',
            ])
            ->addColumn('credential', 'string', [
                'limit' => 255,
                'null' => true,
                'comment' => '凭证（密码哈希/授权码等）',
            ])
            ->addColumn('metadata', 'json', [
                'null' => true,
                'comment' => '附加元数据',
            ])
            ->addColumn('unionid', 'string', [
                'limit' => 255,
                'null' => true,
                'comment' => '跨平台唯一标识',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-启用',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['identity_type', 'identifier'], ['unique' => true])
            ->addIndex(['user_id'])
            ->create();
    }

    private function createUserRefreshTokenTable(): void
    {
        $this->table('user_refresh_token', [
            'comment' => '刷新令牌表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('jti', 'string', [
                'limit' => 64,
                'comment' => '令牌唯一标识',
            ])
            ->addColumn('device_fingerprint', 'string', [
                'limit' => 120,
                'null' => true,
                'comment' => '设备指纹',
            ])
            ->addColumn('expires_time', 'datetime', [
                'comment' => '过期时间',
            ])
            ->addColumn('meta', 'json', [
                'null' => true,
                'comment' => '扩展数据',
            ])
            ->addColumn('revoked_time', 'datetime', [
                'null' => true,
                'comment' => '撤销时间',
            ])
            ->addColumn('revoked_reason', 'string', [
                'limit' => 60,
                'null' => true,
                'comment' => '撤销原因',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-正常 2-撤销',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['jti'], ['unique' => true])
            ->addIndex(['user_id'])
            ->addIndex(['device_fingerprint'])
            ->addIndex(['expires_time'])
            ->create();
    }

    private function createUserVerificationCaseTable(): void
    {
        $this->table('user_verification_case', [
            'comment' => '认证申请流水',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '申请人用户ID',
            ])
            ->addColumn('type', 'string', [
                'limit' => 20,
                'comment' => '认证类型 personal/company',
            ])
            ->addColumn('status', 'string', [
                'limit' => 20,
                'default' => 'pending',
                'comment' => '状态 pending/approved/rejected',
            ])
            ->addColumn('region_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '关联区域ID',
            ])
            ->addColumn('inviter_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '邀请人用户ID',
            ])
            ->addColumn('submitted_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '提交人（默认等于 user_id）',
            ])
            ->addColumn('reviewed_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '审核人用户ID',
            ])
            ->addColumn('submitted_at', 'timestamp', [
                'null' => true,
                'comment' => '提交时间',
            ])
            ->addColumn('reviewed_at', 'timestamp', [
                'null' => true,
                'comment' => '审核时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '审核备注',
            ])
            ->addColumn('payload', 'json', [
                'null' => true,
                'comment' => '提交原始数据',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['user_id'])
            ->addIndex(['type'])
            ->addIndex(['status'])
            ->addIndex(['region_id'])
            ->create();
    }

    private function createUserIdentityTable(): void
    {
        $this->table('user_identity', [
            'comment' => '用户业务身份',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('identity_type', 'string', [
                'limit' => 30,
                'comment' => '身份类型 admin/support/seller/buyer 等',
            ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '关联组织ID',
            ])
            ->addColumn('region_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '关联区域ID',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '状态 0-待生效 1-有效 2-禁用',
            ])
            ->addColumn('source', 'string', [
                'limit' => 20,
                'default' => '',
                'comment' => '来源 personal/company/system',
            ])
            ->addColumn('verification_case_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '关联认证申请ID',
            ])
            ->addColumn('invited_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '邀请人用户ID',
            ])
            ->addColumn('verified_at', 'timestamp', [
                'null' => true,
                'comment' => '生效时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['user_id'])
            ->addIndex(['identity_type'])
            ->addIndex(['org_id'])
            ->addIndex(['region_id'])
            ->addIndex(['verification_case_id'])
            ->addIndex(['user_id', 'identity_type'], ['unique' => true])
            ->create();
    }

    private function createUserIndividualTable(): void
    {
        $this->table('user_individual', [
            'comment' => '个人实名认证',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('verification_case_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '关联认证申请ID',
            ])
            ->addColumn('real_name', 'string', [
                'limit' => 50,
                'comment' => '真实姓名',
            ])
            ->addColumn('id_card', 'string', [
                'limit' => 32,
                'comment' => '身份证号',
            ])
            ->addColumn('id_card_front', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '身份证正面',
            ])
            ->addColumn('id_card_back', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '身份证反面',
            ])
            ->addColumn('valid_from', 'date', [
                'null' => true,
                'comment' => '证件有效期开始',
            ])
            ->addColumn('valid_to', 'date', [
                'null' => true,
                'comment' => '证件有效期结束',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '状态 0-待审核 1-通过 2-驳回',
            ])
            ->addColumn('submitted_at', 'timestamp', [
                'null' => true,
                'comment' => '提交时间',
            ])
            ->addColumn('verified_at', 'timestamp', [
                'null' => true,
                'comment' => '审核时间',
            ])
            ->addColumn('verified_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '审核人',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '审核备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['user_id'], ['unique' => true])
            ->create();
    }

    private function createUserCompanyTable(): void
    {
        $this->table('user_company', [
            'comment' => '企业认证',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('verification_case_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '关联认证申请ID',
            ])
            ->addColumn('company_name', 'string', [
                'limit' => 150,
                'comment' => '企业名称',
            ])
            ->addColumn('license_code', 'string', [
                'limit' => 64,
                'comment' => '统一社会信用代码',
            ])
            ->addColumn('license_photo', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '营业执照图片',
            ])
            ->addColumn('legal_rep', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '法人代表',
            ])
            ->addColumn('address', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '注册地址',
            ])
            ->addColumn('scope', 'text', [
                'null' => true,
                'comment' => '经营范围',
            ])
            ->addColumn('region_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '所属区域',
            ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '关联组织ID',
            ])
            ->addColumn('inviter_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '邀请人',
            ])
            ->addColumn('extra', 'json', [
                'null' => true,
                'comment' => '扩展信息（发票、地址等）',
            ])
            ->addColumn('ocr_raw', 'json', [
                'null' => true,
                'comment' => 'OCR 原始数据',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '状态 0-待审核 1-通过 2-驳回',
            ])
            ->addColumn('submitted_at', 'timestamp', [
                'null' => true,
                'comment' => '提交时间',
            ])
            ->addColumn('verified_at', 'timestamp', [
                'null' => true,
                'comment' => '审核时间',
            ])
            ->addColumn('verified_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '审核人',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '审核备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['user_id'], ['unique' => true])
            ->addIndex(['company_name'])
            ->addIndex(['license_code'], ['unique' => true])
            ->addIndex(['org_id'])
            ->addIndex(['status'])
            ->create();
    }
}
